﻿using System;

namespace Selenium.PageToolkit.ControlFactoryChainImpl
{
    internal class ControlFactory : ChainBase<PageElementAttribute>
    {
        protected override object Inject(Type fieldType, PageElementAttribute attribute, LambdaPageFactory factory)
        {
            if (fieldType.GetConstructor(new Type[0]) == null)
                return null;
            
            var inst = Activator.CreateInstance(fieldType);
            new LambdaPageFactory() { DriverFactory = () => factory.DriverFactory().FindElement(attribute.Spec()) }.Inject(inst);
            return inst;
        }
    }
}